package com.gitee.feizns.explore.data_structure.tree.bs.mid;

/**
 * 623. 在二叉树中增加一行
 * @author feizns
 * @since 2020/3/10
 */
public class AddOneRow {

    public static void main(String[] args) {

    }

    public TreeNode addOneRow(TreeNode root, int v, int d) {
        return addOneRow(root, v, d, 1, true);
    }

    public static TreeNode addOneRow(TreeNode root, int v, int d, int death, boolean isLeft) {
        if ( d == death ) {
            TreeNode ret = new TreeNode(v);
            if ( isLeft )
                ret.left = root;
            else
                ret.right = root;
            return ret;
        }
        if ( root != null ) {
            root.left = addOneRow(root.left, v, d, death + 1, true);
            root.right = addOneRow(root.right, v, d, death + 1, false);
        }
        return root;
    }

}
